<template>
	<div class="WorkOrdermaintenance">
		<div class="cont">
			<div class="cont-left">
				<div class="BasicInformation">
					<div class="title">
						基本信息
					</div>
					<div class="info">
						<el-row :gutter="10">
							<el-col :span="12">
								<el-form-item label="产品编号：">
									<el-input placeholder="请输入产品编号" v-model="from.input.invCode" @keyup.enter="selectData" />
								</el-form-item>
							</el-col>
							<el-col :span="12">
								<el-form-item label="制 令 号 ：">
									<el-input  placeholder="请输入制令号" v-model="from.input.mocCode" @keyup.enter="selectData" />
								</el-form-item>
							</el-col>
							<el-col :span="24">
								<div style="width: 100%;height: 100%;display: flex;justify-content: center;align-items: center;margin-top: -10px;">
									<hr width="100%" />
								</div>
							</el-col>
							<el-col :span="12">
								<el-form-item label="产品名称：">
									<el-input disabled v-model="from.show.fInvName" />
								</el-form-item>
							</el-col>
							<el-col :span="12">
								<el-form-item label="产品型号：">
									<el-input  disabled v-model="from.show.fInvStd" />
								</el-form-item>
							</el-col>
							<el-col :span="24">
								<el-form-item label="规格描述：">
									<el-input  disabled v-model="from.show.fInvRemark" />
								</el-form-item>
							</el-col>
							<el-col :span="24">
								<el-button type="primary" @click="pullRW(true)">
									写入制令信息
								</el-button>
								<el-button type="primary" @click="pullRW(false)">
									初始化产品
								</el-button>
							</el-col>
						</el-row>
					</div>
				</div>
				<div class="TechnologyInf">
					<div class="title">
						产品工艺信息
					</div>
					<div class="info">
						<el-row :gutter="10">
							<el-col :span="24">
								<div class="table-head-botton">
									<button>扫描件维护</button>
								</div>
								  <el-table :data="data.le" border style="width: 100%" v-loading="loding.le"
                            @selection-change="(ev)=>{chaeckDel(ev,0)}">
									<el-table-column type="selection" width="40"/>
									<el-table-column align="center" label="序号" #default="scope" width="80" :show-overflow-tooltip="true">
										{{scope.$index+1}}
									</el-table-column>
									<el-table-column align="center" prop="fInvCode" label="物料编号"  min-width="120" :show-overflow-tooltip="true"/>
									<el-table-column align="center" prop="fclnvName" label="物料名称"  min-width="120" :show-overflow-tooltip="true" />
									<el-table-column align="center" prop="fclnvStd" label="物料型号" min-width="120" :show-overflow-tooltip="true" />
									<el-table-column align="center" prop="fScaPro" label="扫描工序" min-width="120" :show-overflow-tooltip="true" />
									<el-table-column align="center" label="扫描工序" #default="scope" width="200" :show-overflow-tooltip="true">
									<el-input v-model="data.le[scope.$index].fScaPro" autocomplete="off">
										<template #append>
											<span @click="popUpShow.chiadPopup=true,indexs.nowrit=(scope.$index),data.isRit=false" style="cursor: pointer;width: 100%;height: 100%;font-weight: 800;font-size: 0.9375rem;">
												…
											</span>
										</template>
									</el-input>
									</el-table-column>
									<el-table-column align="center" prop="fProName" label="工序名称" min-width="120" :show-overflow-tooltip="true" />
									<el-table-column align="center" prop="fQty" label="单套数量" min-width="120" :show-overflow-tooltip="true" />
									<el-table-column align="center" label="操作" #default="scope" width="180" fixed="right" :show-overflow-tooltip="true">
										<el-button type="primary" @click="edit(scope,0,true)">重置</el-button>
										<el-button type="primary" @click="edit(scope,0,false)">修改</el-button>
									</el-table-column>
								</el-table>
							</el-col>
							<el-col :span="24">
								<div style="width: 100%;display: flex;align-items: center;justify-content: flex-end;">
									<el-button type="danger" @click="dels(0)" v-show='delList.le.length>0'>批量删除</el-button>
								</div>
							</el-col>
						</el-row>
					</div>
				</div>
			</div>
			<div class="cont-rit">
				<div class="table">
					<div class="title">

					</div>
					<div class="info">
						<el-row :gutter="10">
							<el-col :span="24">
								  <el-table :data="data.rit" border style="width: 100%" v-loading="loding.rit" tooltip-effect="light">
									<el-table-column align="center" prop="num" label="序号" :show-overflow-tooltip="true" />
									<el-table-column align="center" prop="fMoId" label="工单单号" :show-overflow-tooltip="true"/>
									<el-table-column align="center" prop="fMocCode" label="工单编号" width="120" :show-overflow-tooltip="true" />
									<el-table-column align="center" prop="fMoDId" label="子单单号" width="120" :show-overflow-tooltip="true" />
									<el-table-column align="center" prop="fQty" label="预计产量" :show-overflow-tooltip="true" />
									<el-table-column align="center" prop="fInvCode" label="产品品号" min-width="120" :show-overflow-tooltip="true" />
									<el-table-column align="center" prop="fInvName" label="产品名称" min-width="120" :show-overflow-tooltip="true" />
									<el-table-column align="center" prop="fInvStd" label="产品规格" min-width="120" :show-overflow-tooltip="true"/>
									<el-table-column align="center" prop="fInvRemark" label="产品规格描述" width="120" :show-overflow-tooltip="true" />
									<el-table-column align="center" prop="fcSubSysBarCode" label="条码号" width="120" :show-overflow-tooltip="true" />
									<el-table-column align="center" prop="fProRemark" label="备注" min-width="200" :show-overflow-tooltip="true" />
									<el-table-column fixed="right" align="center" label="操作" width="120" >
										<template #default="scope">
												<el-button :type="(scope.$index==data.ritNowIndex)?'':'primary'"  @click="select(scope.row,scope.$index)">
													选择
												</el-button>
										</template>
									</el-table-column>
								  </el-table>
							</el-col>
						</el-row>
					</div>
				</div>
				<div class="table">
					<div class="title">

					</div>
					<div class="info">
						<el-row :gutter="10">
							<el-col :span="24">
								<el-table :data="data.rib" border style="width: 100%" v-loading="loding.rib" @selection-change="(ev)=>{chaeckDel(ev,1)}" >
									<el-table-column type="selection" width="40"/>
									<el-table-column align="center" label="序号" #default="scope" width="80" :show-overflow-tooltip="true">
										{{scope.$index+1}}
									</el-table-column>
									<el-table-column align="center" prop="fInvCode" label="物料编号"  min-width="120" :show-overflow-tooltip="true"/>
									<el-table-column align="center" prop="fclnvName" label="物料名称"  min-width="120" :show-overflow-tooltip="true" />
									<el-table-column align="center" label="扫描工序" #default="scope" width="200" :show-overflow-tooltip="true">
									<el-input v-model="data.rib[scope.$index].fScaPro" autocomplete="off">
										<template #append>
											<span @click="popUpShow.chiadPopup=true,indexs.nowrit=(scope.$index),data.isRit=true" style="cursor: pointer;width: 100%;height: 100%;font-weight: 800;font-size: 0.9375rem;">
												…
											</span>
										</template>
									</el-input>
									</el-table-column>
									<el-table-column align="center" prop="fclnvStd" label="物料型号" min-width="120" :show-overflow-tooltip="true" />
									<el-table-column align="center" prop="fProName" label="工序名称" min-width="120" :show-overflow-tooltip="true" />
									<el-table-column align="center" prop="fQty" label="单套数量" min-width="120" :show-overflow-tooltip="true" />
									<el-table-column align="center" label="操作" #default="scope" width="180" fixed="right" :show-overflow-tooltip="true">
										<el-button type="primary" @click="edit(scope,1,true)">重置</el-button>
										<el-button type="primary" @click="edit(scope,1,false)">修改</el-button>
									</el-table-column>
								</el-table>
							</el-col>
							<el-col :span="24">
								<div style="width: 100%;display: flex;align-items: center;justify-content: flex-end;">
									<el-button type="danger" @click="dels(1)" v-show='delList.rit.length>0'>批量删除</el-button>
								</div>
							</el-col>
						</el-row>
					</div>
				</div>
			</div>
		</div>
	</div>
	<div class="footName">
<!-- 		<el-row :gutter="10">
			<el-col :span="1.5">
				录入人：
			</el-col>
			<el-col :span="2">
				<el-form-item>
					<el-input />
				</el-form-item>
			</el-col>
			<el-col :span="2">
				<el-form-item>
					<el-input />
				</el-form-item>
			</el-col>
		</el-row> -->
	</div>
	<el-dialog v-model="popUpShow.chiadPopup" title="选择" >
		<div class="select-cont">
			<div class="selecetHead">
				<el-row :gutter="10">
					<el-col :span="5">
					  <el-select @change="jdSelect" v-model="pageInf.chid.pagesize" class="m-2" placeholder="Select">
						<el-option v-for="i in 10" :key="10*i" :label="'显示'+(10*i)+'条'" :value="(10*i)" />
						</el-select>
					</el-col>
					<el-col :span="4">
						总条数:{{pageInf.chid.sumPage}}
					</el-col>
					<el-col :span="5">
						<el-input v-model="data.chidName" @keyup.enter="jdSelect"></el-input>
					</el-col>
					<!-- <el-col :span="3">
						<el-button type="primary" plain @click="jdSelect">
							简单查询
						</el-button>
					</el-col>
					<el-col :span="3">
						<el-button type="primary" plain>
							方案查询
						</el-button>
					</el-col> -->
				</el-row>
			</div>
			<div class="tab">
				<!-- <div class="li">
					查询结果
				</div>
				<div class="li">
					查询方案
				</div> -->
			</div>
			<div class="table">
				<el-row :gutter="10">
					<el-col :span="24">
						<el-table :data="data.chidTable" border style="width: 100%;height: 40vh;">
							<el-table-column prop="fCode" label="工序编号" />
							<el-table-column prop="fName" label="工序名称" />
							<el-table-column prop="fId" label="内码" />
							<el-table-column fixed="right" label="操作" width="80" align="center">
								<template #default="{ row }">
									<el-button type="primary" size="small" @click="onHandleEdit(row)">选择</el-button>
								</template>
							</el-table-column>
						</el-table>
					</el-col>
				</el-row>
			</div>
		</div>
	</el-dialog>
</template>

<script setup>
	import ajax from "../../../../public/JavaScript/api/ajax.js";
	import { ref,reactive,onMounted,watch } from 'vue';
	import { ElMessageBox,ElMessage } from "element-plus";
	onMounted(()=>{
		jdSelect()
	})
	let data=reactive({
		le:[],
		rit:[],
		rib:[],
		chidTable:[],
		ritNowIndex:0,
		chidTC:false,
		chidName:'',
		isRit:true	//是左边还是右边点击了编辑
	})
	let pageInf=reactive({
		chid:{
			pagesize:10,
			sumPage:0
		}
	})
	let loding=reactive({
		le:false,
		rit:false,
		rib:false
	})
	/* 多选框列表 */
	let delList=reactive({
		le:[],
		rit:[]
	})
  const fids = ref("");
  const f = ref([]);
	/* 操作多选框 */
	function chaeckDel(ev,type){
		console.log(ev,type);
    f.value = [];
    f.value = ev;
    fids.value = "";
    let ids = "";
    for (let i = 0; i < ev.length; i++) {
      ids += ev[i].fId + ',';
    }
    fids.value = ids;
		if(type==0){
			delList.le=ev
		}else{
			delList.rit=ev
		}
	};
	/* 删除多选数据 */
	function dels(type){
		ElMessageBox.confirm(
		  '是否确认删除?',
		  '警告',
		  {
			confirmButtonText: '是',
			cancelButtonText: '否',
			type: 'error',
		  }
		)
		  .then(() => {
			if(type==0){
				let arr=[]
				for(let i in delList.le){
					arr.push(delList.le[i].fId)
				}
				ajax.post('/pmc/ri?ids='+arr.join(','),{body:{}},res=>{
					if(res.code==200){
						ElMessage.success('删除成功')
						data.le=[]
						delList.le=[]
						selectCp()
					}else{
						ElMessage.error(res.msg)
					}
				},err=>{
					ElMessage.error('服务器错误')
				})
			}else{
				let arr=[]
				for(let i in delList.rit){
					arr.push(delList.rit[i].fId)
				}
				ajax.post('/pmc/rm?ids='+arr.join(','),{body:{}},res=>{
					if(res.code==200){
						ElMessage.success('删除成功')
						data.rit=[]
						delList.rit=[]
						selectZl()
					}else{
						ElMessage.error(res.msg)
					}
				},err=>{
					ElMessage.error('服务器错误')
				})
			}
		  })

	}
	/* 单条修改 */
	function edit(ev,type,isRset){
		ElMessageBox.confirm(
		  '是否修改?',
		  '警告',
		  {
		    confirmButtonText: '是',
		    cancelButtonText: '否',
		    type: 'error',
		  }
		)
		  .then(() => {
			if(type==0){
				if(isRset){
					ajax.post('/pmc/mii',{
						body:{
						  "code": "",
						  "id": ev.row.fId,
						  "name": ""
						}
					},res=>{
						if(res.code==200){
							ElMessage.success('重置成功')
							data.le[ev.$index].fScaPro=''
							data.le[ev.$index].fProName=''
						}else{
							ElMessage.error(res.msg)
						}
					},err=>{
						ElMessage.error('服务器异常')
					})
				}else{
					ajax.post('/pmc/mii',{
						body:{
						  "code": ev.row.fScaPro,
						  "id": ev.row.fId,
						  "name": ev.row.fProName
						}
					},res=>{
						if(res.code==200){
							ElMessage.success('修改成功')
						}else{
							ElMessage.error(res.msg)
						}
					},err=>{
						ElMessage.error('服务器异常')
					})
				}
			}else{
				if(isRset){
					ajax.post('/pmc/mmi',{
						body:{
						  "code": "",
						  "id": ev.row.fId,
						  "name": ""
						}
					},res=>{
						if(res.code==200){
							ElMessage.success('重置成功')
							data.rib[ev.$index].fScaPro=''
							data.rib[ev.$index].fProName=''
						}else{
							ElMessage.error(res.msg)
						}
					},err=>{
						ElMessage.error('服务器异常')
					})
				}else{
					ajax.post('/pmc/mmi',{
						body:{
						  "code": ev.row.fScaPro,
						  "id": ev.row.fId,
						  "name": ev.row.fProName
						}
					},res=>{
						if(res.code==200){
							ElMessage.success('修改成功')
						}else{
							ElMessage.error(res.msg)
						}
					},err=>{
						ElMessage.error('服务器异常')
					})
				}
			}
		  })
		console.log(ev.row)
	}
	let popUpShow = reactive({
		aditPopup: false, //编辑弹窗是否显示
		addPopup: false ,//新增弹窗是否显示
		chiadPopup:false	//子弹窗是否显示
	})
	let from=reactive({
		input:{
			invCode:'',	//产品编号
			mocCode	:''	//制令号
		},
		show:{
			"clnvDefine7": "",
			"fInvCode": "",
			"fInvName": "",
			"fInvRemark": "",
			"fInvStd": "",
			"fMoDId": 0,
			"fMoId": 0,
			"fMocCode": "",
			"fQty": 0,
			"num": 0,
			"snBarCode": ""
		}
	})
	/* 查询 */
	function selectData(){
		/* 查询之前先初始化 */
		data.le=[]
		data.rib=[]
		delList.le=[]
		delList.rit=[]

		loding.rit=true
		console.log(from.input)
		ajax.post("/pmc/bmo?invCode="+from.input.invCode+"&mocCode="+from.input.mocCode,{
			body:{}
		},res=>{
			console.log(res)
			if(res.code==200){
				if(res.data.length==0){
					ElMessage.error('数据库数据为空！')
				}else{
					ElMessage.success("查询成功！")
					data.rit=res.data
					select(null)
				}
			}else{
				ElMessage.error(res.msg)
			}
			loding.rit=false
			from.input.invCode=''
			from.input.mocCode=''
		},err=>{
			ElMessage.error("网络连接失败！")
			loding.rit=false
			from.input.invCode=''
			from.input.mocCode=''
		})
	}
	/* 查询产品 */
	async function selectCp(){
		loding.le=true
		ajax.post("/pmc/pmbi?invCode="+from.show.fInvCode,{
			body:{}
		},res=>{
			console.log(res)
			if(res.code==200){
				if(res.data.length==0){
					// ElMessage.error('数据库数据为空！')
				}else{
					// ElMessage.success("查询成功！")
					data.le=res.data
				}
			}else{
				ElMessage.error(res.msg)
			}
			loding.le=false
		},err=>{
			ElMessage.error("网络连接失败！")
			loding.le=false
			loding.rib=false
		})
	}
	/* 查询制令 */
	async function selectZl(){
		loding.rib=true
		ajax.post("/pmc/pmbm?mocCode="+from.show.fMocCode,{
			body:{}
		},res=>{
			console.log(res)
			if(res.code==200){
				if(res.data.length==0){
					// ElMessage.error('数据库数据为空！')
				}else{
					// ElMessage.success("查询成功！")
					data.rib=res.data
				}
			}else{
				ElMessage.error(res.msg)
			}
			loding.rib=false
		},err=>{
			ElMessage.error("网络连接失败！")
			loding.rib=false
		})
	}
	function select(ev,index){
		console.log(ev,index)
		data.ritNowIndex=index
		if(ev==null){
			sCOPY(from.show,data.rit[0])
		}else{
			sCOPY(from.show,ev)
		}
		selectCp()
		selectZl()
	}
	/* 深拷贝 */
	function sCOPY(start,end){
		for(let i in end){
			start[i]=JSON.parse(JSON.stringify(end[i]))
		}
	}
	/* 对象赋空 */
	function clearObj(obj){
		for(let i in obj){
			obj[i]=''
		}
	}
	function jdSelect(){		//简单查询
		data.chidTC=true
		ajax.post('/wpc/simjsp',{
			body:{
			  "fName": data.chidName,
			  "page": 1,
			  "size": pageInf.chid.pagesize
			}
		},res=>{
			console.log(res)
			if(res.code==200){
				ElMessage({
					message: '查询成功！',
					type: 'success',
				})
				data.chidTC=false
				if(res.data.totalCount==0){
					ElMessage.error('查询数据列表为空！')
					data.chidTC=false
				}
				data.chidTable=res.data.list
				data.chidName=''
				pageInf.chid.sumPage=res.data.totalCount
			}else{
				ElMessage.error(res.msg)
				data.chidTC=false
			}
		},err=>{
			ElMessage.error(err.msg)
			data.chidTC=false
		})
	}
	let indexs=reactive({
		nowrit:0
	})
	function onHandleEdit(ev) {
		console.log(ev)
		if(data.isRit){
			data.rib[indexs.nowrit].fScaPro=ev.fCode
			data.rib[indexs.nowrit].fProName=ev.fName
			popUpShow.chiadPopup=false
		}else{
			data.le[indexs.nowrit].fScaPro=ev.fCode
			data.le[indexs.nowrit].fProName=ev.fName
			popUpShow.chiadPopup=false
		}
	}
	/* 写入制令 */
	function pullRW(type){
		console.log({
				  "invCode":from.show.fInvCode ,
				  "mocCode": from.show.fMocCode,
				  "processMaterialsList": data.rib,
				  "processMaterialsPreserveList": data.le
			})
		ajax.post((type?'/pmc/wmi':'/pmc/wii'),{
			body:{
				  "invCode":from.show.fInvCode ,
				  "mocCode": from.show.fMocCode,
				  "processMaterialsList": data.rib,
				  "processMaterialsPreserveList": data.le
			}
		},res=>{
			console.log(res)
			if(res.code==200){
				ElMessage.success((type?'写入成功！':'初始化成功！'))
				/* 初始化表格数据 */
				data.le=[]
				data.rib=[]
				delList.le=[]
				delList.rit=[]

				data.rib=res.data.processMaterialsList
				data.le=res.data.processMaterialsPreserveList
			}else{
				ElMessage.error(res.msg)
			}
		},err=>{
			console.log(err)
			ElMessage.error(err.msg)
		})
	}
</script>

<style lang="less" scoped>
	@import url("./WorkOrdermaintenance.less");
</style>